package com.atguigu.springcloud.大批量任务处理.util;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.function.Consumer;

/**
 * @author XiongJianLin
 * @date Created in 2025/2/10 15:35
 */
public class TaskBatchSendUtil {
    public static <T> void send(List<T> taskList, Executor threadPool, Consumer<? super T> consumer) throws InterruptedException {
        if (taskList == null || taskList.isEmpty()){
            return;
        }
        if (Objects.isNull(consumer)){
            return;
        }
        CountDownLatch countDownLatch = new CountDownLatch(taskList.size());
        for (T msg : taskList) {
            threadPool.execute(()->{
                try {
                    consumer.accept(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    countDownLatch.countDown();
                }
            });
        }
        countDownLatch.await();
    }

    /**
     *  void 方法，只有输入参数，适配consumer.accept(T t)
     * @param task
     */
    public static void disposeTask(String task){
        // 实际调用。。。。
        System.out.println(String.format("[%s] disposeTask下发优惠券或短信 OK",task));
    }

    public static void disposeMailTask(String task){
        System.out.println(String.format("[%s] disposeTask下邮件 OK",task));
    }
}
